/*****************************************************************************
 *  Copyright [2008] [fuzzylogics]                                            *
 *                                                                            *
 *  Licensed under the Apache License, Version 2.0 (the "License");           *
 *  you may not use this file except in compliance with the License.          *
 *  You may obtain a copy of the License at                                   *
 *                                                                            *
 *      http://www.apache.org/licenses/LICENSE-2.0                            *
 *                                                                            *
 *  Unless required by applicable law or agreed to in writing, software       *
 *  distributed under the License is distributed on an "AS IS" BASIS,         *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *  See the License for the specific language governing permissions and       *
 *  limitations under the License.                                            *
 ******************************************************************************/
package it.fuzzylogics.fuzzywidgetsLibrary.client.FuzzyClosableTab;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;
import it.fuzzylogics.fuzzywidgetsLibrary.client.FuzzyUtils.FUniqueElementID;

public abstract class FClosableTabItem extends ComplexPanel {

    FClosableTabItem me = this;
    HTML closeHTML = null;
    int $width, $height;

    public FClosableTabItem(Widget content, boolean Closable, int width, int height) throws Exception {
        $width = width;
        $height = height;

        if ($width <= 0 || $height <= 0)
            throw new Exception("the width and height can not be negative or equal 0");

        initClosableTabItem(content, Closable, width, height, 0);
    }

    public FClosableTabItem(Widget content, boolean Closable, int width, int height, int marginTopPight) throws Exception {
        $width = width;
        $height = height;

        if ($width <= 0 || $height <= 0)
            throw new Exception("the width and height can not be negative or equal 0");

        initClosableTabItem(content, Closable, width, height, marginTopPight);
    }

    public void initClosableTabItem(Widget content, boolean Closable, int width, int height, int marginTopPight) {

        setElement(DOM.createDiv());
        DOM.setStyleAttribute(getElement(), "position", "relative");
        DOM.setElementAttribute(getElement(), "id", "" + FUniqueElementID.getInstance().getRandomID(65535));
        setPixelSize(width, height);

        sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);


        closeHTML = new HTML();
        closeHTML.setStylePrimaryName("FClosableTabItem-closeButton");
        closeHTML.setVisible(false);
        closeHTML.setTitle("Chiudi " + content.getTitle());
        closeHTML.addClickListener(new ClickListener() {

            public void onClick(Widget widget) {

                closeEvent(me);

            }
        });
        closeHTML.addMouseListener(new MouseListener() {

            public void onMouseDown(Widget widget, int i, int i1) {

            }

            public void onMouseEnter(Widget widget) {
                setCssMouseOn(true);
            }

            public void onMouseLeave(Widget widget) {
                setCssMouseOn(false);
            }

            public void onMouseMove(Widget widget, int i, int i1) {

            }

            public void onMouseUp(Widget widget, int i, int i1) {

            }
        });


        add(content, 0, 0);

        if (Closable)
            add(closeHTML, width - 12 - marginTopPight, marginTopPight);

    }

    public void setCssMouseOn(boolean mouseOn) {


        String mouseOnStyle = "on";

        if (mouseOn) {
            closeHTML.addStyleDependentName(mouseOnStyle);
        } else {
            closeHTML.removeStyleDependentName(mouseOnStyle);
        }
    }

    private static void changeToStaticPositioning(Element elem) {
        DOM.setStyleAttribute(elem, "left", "");
        DOM.setStyleAttribute(elem, "top", "");
        DOM.setStyleAttribute(elem, "position", "");
    }

    public void add(Widget w) {
        super.add(w, getElement());
    }

    public void add(Widget w, int left, int top) {
        w.removeFromParent();
        setWidgetPositionImpl(w, left, top);
        add(w);
    }

    public boolean remove(Widget w) {
        boolean removed = super.remove(w);
        if (removed) {
            changeToStaticPositioning(w.getElement());
        }
        return removed;
    }

    public void setWidgetPosition(Widget w, int left, int top) {
        setWidgetPositionImpl(w, left, top);
    }

    private void setWidgetPositionImpl(Widget w, int left, int top) {
        Element h = w.getElement();

        DOM.setStyleAttribute(h, "position", "absolute");
        DOM.setStyleAttribute(h, "left", left + "px");
        DOM.setStyleAttribute(h, "top", top + "px");
    }

    public void onBrowserEvent(Event e) {
        switch (DOM.eventGetType(e)) {
            case Event.ONMOUSEOVER: {
                closeHTML.setVisible(true);
                break;
            }
            case Event.ONMOUSEOUT: {
                closeHTML.setVisible(false);
                break;
            }
        }
    }

    public abstract void closeEvent(Widget me);

}
